import { defineStore } from "pinia";
import { ref, reactive } from "vue"
import { ElMessage } from "element-plus";
import { useEnumStore } from "@/store/enumeration"

import { queryOrderTransformDetailApiPost } from "@/api/allProduct"
import useInstance from '@/hooks/useInstance';
export const orderDetailProStore = defineStore("orderDetailProStoreId", () => {
    const { global } = useInstance();
    const addAndEditRef = ref<{ show: (title: string, row?: any) => void }>()


    const detailData = ref([
        {
            type: "text",
            prop: "channelKey",
            name: "渠道Key",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "orderDetailPro",
            propsList: [

                {
                    prop: 'channelId',
                    label: '渠道id',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'channelName',
                    label: '渠道名称',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'orderNo',
                    label: '订单号',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'mobile',
                    label: '手机号',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'createTime',
                    label: '创建时间',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'transformTime',
                    label: '转化时间',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'transformStayTime',
                    label: '转化停留时长',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'transformState',
                    label: '转化状态',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'realState',
                    label: '真实状态',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'productName',
                    label: '产品名称',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                }

            ]
        })

    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            productCode: null, // 产品编码
            productType: null, // 产品类型
            productCategoryCode: null, // 产品大类
            channelCode: null, // 渠道编码
            supplierCode:null,
            isQueryChannelStatistics:null
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })
    const getDate1 = async (type: string) => {
        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                if (it != 'productCode' || it != 'channelCode') {
                    // @ts-ignore
                    listSearch.condition[it] = null
                }
            })
        }

        try {
            const res = await queryOrderTransformDetailApiPost(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item
                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false

        }
    }

    return {
        detailData, tableProp, listSearch, pageParams, addAndEditRef,
        getDate1,
    }
})